package com.timoon.component.panel.jqgrid;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.timoon.app.utils.Constantor;
import com.timoon.component.panel.base.BaseController;
import com.timoon.component.util.ComponentConstant;


/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
@Controller
@RequestMapping(value="jqgrid")
public class JqGridController extends BaseController {
	
	
	@ResponseBody
	@RequestMapping(value="/getGridData")
	public void getGridData(HttpServletRequest request, HttpServletResponse response){
		Map<String,JqGridPanel> map=(Map<String, JqGridPanel>) request.getSession().getAttribute(ComponentConstant.JQGRID_PANEL_MAP_IN_SESSION);
		JqGridPanel panel=map.get(request.getParameter("gridKey"));
		
		this.write(response,panel.getService().getGridData(request));
	};
	
	@ResponseBody
	@RequestMapping(value="/onDelete")
	public void onDelete(HttpServletRequest request, HttpServletResponse response){
		Map<String,JqGridPanel> map=(Map<String, JqGridPanel>) request.getSession().getAttribute(ComponentConstant.JQGRID_PANEL_MAP_IN_SESSION);
		JqGridPanel panel=map.get(request.getParameter("gridKey"));
		
		panel.onDelete(request.getParameter("deleteIds"));
		this.writeMsg(response, true, "删除成功");
	}
	@ResponseBody
	@RequestMapping(value="/setDetailsId")
	/**
	 * 客户选择了一条记录查看明细，此方法的作用是把客户端选中的detailsId传送给DetailsButton
	 * @param request.detailsId		验证过的欲查看明细的id
	 * @param response
	 */
	public void setDetailsId(HttpServletRequest request, HttpServletResponse response){
		Map<String,JqGridPanel> map=(Map<String, JqGridPanel>) request.getSession().getAttribute(ComponentConstant.JQGRID_PANEL_MAP_IN_SESSION);
		JqGridPanel panel=map.get(request.getParameter("gridKey"));
		
		panel.setDetailsId(request.getParameter("detailsId"));
	}
	@ResponseBody
	@RequestMapping(value="/cancelShowDetails")
	/**
	 * 客户端通知DeatilsButton，由于客户选择的数据不合法，取消查看明细操作
	 * 给DeatilsButton发送detailsId=(-1)，代表取消操作
	 * @param request
	 * @param response
	 */
	public void cancelShowDetails(HttpServletRequest request, HttpServletResponse response){
		Map<String,JqGridPanel> map=(Map<String, JqGridPanel>) request.getSession().getAttribute(ComponentConstant.JQGRID_PANEL_MAP_IN_SESSION);
		JqGridPanel panel=map.get(request.getParameter("gridKey"));
		
		panel.setDetailsId("-1");
	}
}
